package wenjiancaozuo;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileOperations {

    public static void main1(String[] args) {
        // 根目录
        String rootDir = "C:/";

        // (a) 创建多级目录 aaa/bbb/ccc
        File cccDir = new File(rootDir + "aaa/bbb/ccc");
        if (!cccDir.exists()) {
            boolean created = cccDir.mkdirs();
            if (created) {
                System.out.println("Created directories: " + cccDir.getAbsolutePath());
            } else {
                System.out.println("Failed to create directories: " + cccDir.getAbsolutePath());
                return;
            }
        }

        // (b) 删除 ccc 目录
        if (cccDir.exists()) {
            boolean deleted = cccDir.delete();
            if (deleted) {
                System.out.println("Deleted directory: " + cccDir.getAbsolutePath());
            } else {
                System.out.println("Failed to delete directory: " + cccDir.getAbsolutePath());
            }
        }

        // (c) 在 bbb 目录中创建文件 zzz.txt
        File zzzFile = new File(rootDir + "aaa/bbb/zzz.txt");
        try {
            boolean created = zzzFile.createNewFile();
            if (created) {
                System.out.println("Created file: " + zzzFile.getAbsolutePath());
            } else {
                System.out.println("File already exists: " + zzzFile.getAbsolutePath());
            }
        } catch (IOException e) {
            System.out.println("Failed to create file: " + zzzFile.getAbsolutePath());
            e.printStackTrace();
        }

        // (d) 在 zzz.txt 文件中写入一行话，Hello World!
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(zzzFile))) {
            writer.write("Hello World!");
            System.out.println("Successfully wrote to file: " + zzzFile.getAbsolutePath());
        } catch (IOException e) {
            System.out.println("Failed to write to file: " + zzzFile.getAbsolutePath());
            e.printStackTrace();
        }

        // (e) 删除 zzz.txt 文件
        if (zzzFile.exists()) {
            boolean deleted = zzzFile.delete();
            if (deleted) {
                System.out.println("Deleted file: " + zzzFile.getAbsolutePath());
            } else {
                System.out.println("Failed to delete file: " + zzzFile.getAbsolutePath());
            }
        }
    }

    //2.打印C:/WINDOWS/system32下的所有以.dll结尾的文件
    public static void main2(String[] args) {
        // 指定目录
        File directory = new File("C:/WINDOWS/system32");

        // 检查目录是否存在且是目录
        if (directory.exists() && directory.isDirectory()) {
            // 使用文件过滤器获取所有以 .dll 结尾的文件
            FilenameFilter filter = new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".dll");
                }
            };

            // 获取文件列表
            String[] dllFiles = directory.list(filter);

            if (dllFiles != null) {
                for (String fileName : dllFiles) {
                    System.out.println(fileName);
                }
            } else {
                System.out.println("Failed to retrieve .dll files from the directory.");
            }
        } else {
            System.out.println("The specified directory does not exist or is not a directory.");
        }
    }

    public static void main3(String[] args) {
        // 指定目录
        File rootDir = new File("C:/WINDOWS/system32");

        // 检查目录是否存在且是目录
        if (rootDir.exists() && rootDir.isDirectory()) {
            // 调用递归方法打印目录和文件信息
            printDirectoryContents(rootDir, 0);
        } else {
            System.out.println("The specified directory does not exist or is not a directory.");
        }
    }

    // 4.通过递归，打印C:/WINDOWS/system32下及其子目录下的所有目录和文件的信息
    public static void printDirectoryContents(File dir, int level) {
        // 缩进表示当前目录层级
        String indent = " ".repeat(level * 2);

        // 打印当前目录
        System.out.println(indent + "+ [DIR] " + dir.getName());

        // 获取当前目录下的所有文件和子目录
        File[] files = dir.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    // 如果是目录，递归调用
                    printDirectoryContents(file, level + 1);
                } else {
                    // 如果是文件，打印文件信息
                    System.out.println(indent + "  - [FILE] " + file.getName());
                }
            }
        }
    }

    //5.将C:\WINDOWS\Media目录中的所有wav文件复制到桌面的windows_media目录中。
    public static void main(String[] args) {
        // 源目录
        File sourceDir = new File("C:/WINDOWS/Media");

        // 获取桌面路径
        String userHome = System.getProperty("user.home");
        File destDir = new File(userHome, "Desktop/windows_media");

        // 创建目标目录（如果不存在）
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        // 检查源目录是否存在且是目录
        if (sourceDir.exists() && sourceDir.isDirectory()) {
            // 获取源目录下的所有 .wav 文件
            File[] wavFiles = sourceDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".wav"));

            if (wavFiles != null) {
                for (File wavFile : wavFiles) {
                    try {
                        // 定义目标文件的路径
                        Path destFilePath = Paths.get(destDir.getAbsolutePath(), wavFile.getName());
                        // 复制文件到目标目录
                        Files.copy(wavFile.toPath(), destFilePath);
                        System.out.println("Copied: " + wavFile.getName());
                    } catch (IOException e) {
                        System.err.println("Failed to copy: " + wavFile.getName());
                        e.printStackTrace();
                    }
                }
            } else {
                System.out.println("No .wav files found in the source directory.");
            }
        } else {
            System.out.println("The source directory does not exist or is not a directory.");
        }
    }

}
